<script lang="ts" setup>
import { Toast } from 'vant'

const props = withDefaults(defineProps<{
  /** 需要复制的文案 **/
  text?: string | number
}>(), {
  text: ''
})

function copy () {
  if (props.text) {
    const fakeEle = document.createElement('textarea')
    fakeEle.value = String(props.text)
    document.body.appendChild(fakeEle)
    fakeEle.select()
    if (document.execCommand('Copy')) {
      Toast('复制成功')
    } else {
      Toast('复制失败')
    }
    document.body.removeChild(fakeEle)
  }
}
</script>

<template>
  <div @click.stop="copy" class="copy__wrap">
    <slot></slot>
  </div>
</template>

<style scoped>
.copy__wrap {
  display: inline-block;
}
</style>
